---
title: "Replication File for 'Finessing the Hobbesian Dilemma' Paper"
author: "Anonymous for Reviewers"
date: "10/16/2021"
output: 
  pdf_document:
    extra_dependencies: ["tikz"]
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=60),tidy=TRUE)
```

# 1. Preparation: Full Dataset

```{r}

# Given that readxl and DescTools packages are installed, call them using library function

library("readxl")
library("DescTools")
```

Read the .xlsx file into R.

```{r, warning=FALSE}

# Note that I have manually hard copied the data from the master workbook (with lots of formulas) into the single page of a new workbook without any formulas

# VERSION: 20 April 2021

aosa <- read_excel("C:/Users/vanhe/Desktop/Academic Papers/2. Finessing the Hobbesian Dilemma/aosa_dataentry_20Apr2021_Main_forAnalysis.xlsx")

```

Delete all the "Error Check" and "Flag" columns.

```{r}

# This code deletes all columns with "Error Check" and "Flag" in the name

aosa <- aosa[,-c(
                  grep("Error Check", colnames(aosa)),
                  grep("Flag", colnames(aosa))
                )]

```

Make sure the Armed Group External Support variables are all characters.

```{r}

aosa$agextsupport_general_country3 <- as.character(aosa$agextsupport_general_country3)

aosa$agextsupport_general_country4 <- as.character(aosa$agextsupport_general_country4)

aosa$agextsupport_alleged_country4 <- as.character(aosa$agextsupport_alleged_country4)

summary(aosa)

```

## 1.A. Descriptive statistics of "raw" dataset

```{r}

# Earliest year

min(aosa$obsyear)

# Latest year

max(aosa$obsyear)

# Countries

length(unique(aosa$Country))

# Dyads

length(unique(aosa$agname))

# Dyad-Years

length(aosa$agname)

## Total War as % of Dyad-Years

round(
  length(aosa[
    aosa$armedorder == 1,]$agname) 
  /length(aosa$agname), 
  2)

## Containmentas % of Dyad-Years

round(
  length(aosa[
    aosa$armedorder == 2,]$agname) 
  /length(aosa$agname), 
  2)

## Hostilities as % of Dyad-Years

round(
  length(aosa[aosa$armedorder == 3,]$agname) 
  /length(aosa$agname), 
  2)

## Containment as % of Dyad-Years

round(
  length(aosa[aosa$armedorder == 4,]$agname) 
  /length(aosa$agname),
  2)

## Alliance as % of Dyad-Years

round(
  length(aosa[aosa$armedorder == 5,]$agname) 
  /length(aosa$agname),
  2)

# Terminations

sum(aosa$terminate)

## Collapse as % of Terminations

round(
  sum(aosa[aosa$terminateform==1,]$terminate) 
  /sum(aosa$terminate),
  2)

## Incorporation as % of Terminations

round(
  sum(aosa[aosa$terminateform==2,]$terminate) 
  /sum(aosa$terminate),
  2)

## Absorption as % of Terminations

round(
  sum(aosa[aosa$terminateform==3,]$terminate) 
  /sum(aosa$terminate),
  2)

## Disarmament as % of Terminations

round(
  sum(aosa[aosa$terminateform==4,]$terminate) 
  /sum(aosa$terminate),
  2)

## Victory as % of Terminations

round(
  sum(aosa[aosa$terminateform==5,]$terminate) 
  /sum(aosa$terminate),
  2)


```


## 1.B. Initial subsetting

Perform initial subsetting of the aosa dataset according to the research paper's  scope conditions:

* No group-state govt dyads (we only want group-federal govt dyads)

* Observations before 2016

* No Afghanistan, Nepal, or Sri Lanka dyads

Be advised that we will remove mainstream political parties in subsequent code chunk. (We cannot remove them up front because armed groups often transition into and out of this identification in the middle of LLCs)

```{r}

aosa <- aosa[
  aosa[,"state-level"] == 0 
  & aosa[,"obsyear"] <= 2016 
  & ((aosa[,"Country"] != "Afghanistan") 
     & (aosa[,"Country"] != "Nepal") 
     & (aosa[,"Country"] != "Sri Lanka"))
  ,
  ]

```

Now, subset the AOSA dataset to exclude:

* Dyad-years that were not "mainstream political parties"

```{r}

aosa_notParty <- aosa[aosa$agselfID != 1,]

```


# 2. Preparation: List of Long-Term Limited Cooperation Orders

First, we'll perform an initial subset on the aosa dataset:

* At least 5 years of LLC

Simultaneously, we will only keep variables that might be used for analysis.

NOTE: We include mainstream political parties, as some LLC armed groups transition to/from them midway. However, we will eventually exclude LLCs where the armed group is fully a mainstream political party. 

```{r}

aosaLLC_long <- aosa[
  aosa[,"LCfiveyr_filter"] == 1 
  , 
  c("agname",
    "obsyear", 
    "Country", 
    "AG_cleavage", 
    "religion_spec", 
    "ID_ideology", 
    "aggoals", 
    "aggoals2", 
    "agselfID", 
    "agpolwing", 
    "polpar", 
    "pargov", 
    "agillicit", 
    "agtax", 
    "peacedealonset", 
    "peacedealongoing", 
    "ceasefireonset", 
    "ceasefireongoing", 
    "amnesty", 
    "terminate", 
    "agextsupport_general", 
    "agextsupport_alleged"
    )
  ]

```

Second, we will prepare the matrix for further subsetting. We will remove:

* Observations with fewer than 5 years duration of their LLC (this can occur if they had an LLC which extended past 2016)

* Armed groups that were "mainstream political parties" for the duration of the period

```{r}

# First, prepare the variables 


# Armed order variables

## What is the armed group name (armed order name)?
LLCnames <- unique(aosaLLC_long$agname)


# Time variables

## What is the duration of the LLC?
vLLC_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## What year does the LLC start?
vLLC_start <- setNames(vector("list",length(LLCnames)), LLCnames)

## What year does the LLC end?
vLLC_end <- setNames(vector("list",length(LLCnames)), LLCnames)


# Conflict characteristic variables

## In what country does most of the LLC occur?
vCountry <- setNames(vector("list",length(LLCnames)), LLCnames)

## With which cleavage does armed group identify for most of the LLC?
vAG_cleavage <- setNames(vector("list",length(LLCnames)), LLCnames)

## With which religion does the armed group identify for most of the LLC?
vreligion_spec <- setNames(vector("list",length(LLCnames)), LLCnames)

## With which ideology does the armed group identify for most of the LLC?
vID_ideology <- setNames(vector("list",length(LLCnames)), LLCnames)


# Goals of the armed group variables

## For what % of years of the LLC did the armed group espouse at least one maximalist goal?
## aggoals OR aggoals2 == (1) Independence/complete secession OR (3) Control over the central government 
vmaximalGoals_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For what % of years of the LLC did the armed group espouse at least one goal about the territorial periphery?
## aggoals OR aggoals2 == (1) Independence/complete secession OR (2) Autonomy short of secession 
vperipheryGoals_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)


# Party and political activity variables 

## For how many years of the LLC did the armed group NOT identify as "Mainstream Political Party"?
vnotParty_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC did the armed group NOT identify as "Anti-State Rebel"?
vnotRebel_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC did the armed group maintain a political wing?
vagpolwing_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC was the armed group formally involved in politics?
## polpar == (2) formal advocacy OR (3) formal participation
vformalPolpar_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC did the armed group operate some level of parallel government?
## pargov == (2) Semi-organized parallel government OR (3) Fully-organized parallel government
vsomePargov_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC was the armed group involved in illicit economies?
vagillicit_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC was the armed group involved in taxation?
vagtax_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)


# Negotiation variables

## How many peace deals were signed during the LLC?
vpeacedealonsets <- setNames(vector("list",length(LLCnames)), LLCnames)

## How many years of the LLC coincided with a peace deal being in force?
vpeacedeal_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## How many ceasefires were signed during the LLC?
vceasefireonsets <- setNames(vector("list",length(LLCnames)), LLCnames)

## How many years of the LLC coincided with a ceasefire being in force?
vceasefire_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## How many amnesties were anounced by the government during the LLC?
vamnesty_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)


# Termination variable

## Did the armed order terminate during the LLC?
vterminate <- setNames(vector("list",length(LLCnames)), LLCnames)

# External support variables

## For how many years of the LLC did the armed group receive support from another country?
vsupportGen_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)

## For how many years of the LLC did the armed group allegedly receive support from another country?
vsupportAlg_yrs <- setNames(vector("list",length(LLCnames)), LLCnames)


# Second, run the loop to populate the variables with their values

for (ag in LLCnames){
  vLLC_yrs[ag] <- as.numeric(sum(aosaLLC_long$agname==ag))
  vLLC_start[ag] <- as.numeric(min(aosaLLC_long[aosaLLC_long$agname==ag,"obsyear"]))
  vLLC_end[ag] <- as.numeric(max(aosaLLC_long[aosaLLC_long$agname==ag,"obsyear"]))
  
  vCountry[ag] <- sapply(aosaLLC_long[aosaLLC_long$agname==ag,"Country"], Mode)
  vAG_cleavage[ag] <- sapply(aosaLLC_long[aosaLLC_long$agname==ag,"AG_cleavage"], Mode)
  vreligion_spec[ag] <- sapply(aosaLLC_long[aosaLLC_long$agname==ag,"religion_spec"], Mode)
  vID_ideology[ag] <- sapply(aosaLLC_long[aosaLLC_long$agname==ag,"ID_ideology"], Mode)
  
  vmaximalGoals_yrs[ag] <- as.numeric(sum(aosaLLC_long[((aosaLLC_long$aggoals == 1 | aosaLLC_long$aggoals2 == 1)|(aosaLLC_long$aggoals == 3 | aosaLLC_long$aggoals2 == 3)),]$agname==ag))
  vperipheryGoals_yrs[ag] <- as.numeric(sum(aosaLLC_long[((aosaLLC_long$aggoals == 1 | aosaLLC_long$aggoals2 == 1)|(aosaLLC_long$aggoals == 2 | aosaLLC_long$aggoals2 == 2)),]$agname==ag))

  vnotParty_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agselfID!=1,]$agname==ag))
  vnotRebel_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agselfID!=3,]$agname==ag))
  vagpolwing_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agpolwing==1,]$agname==ag, na.rm=TRUE))
  vformalPolpar_yrs[ag] <- as.numeric(sum(aosaLLC_long[(aosaLLC_long$polpar==2 | aosaLLC_long$polpar==3),]$agname==ag, na.rm=TRUE)) 
  
  vsomePargov_yrs[ag] <- as.numeric(sum(aosaLLC_long[(aosaLLC_long$pargov==2 | aosaLLC_long$pargov==3),]$agname==ag, na.rm=TRUE))
  vagillicit_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agillicit==1,]$agname==ag, na.rm=TRUE))
  vagtax_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agtax==1,]$agname==ag, na.rm=TRUE))
  
  vpeacedealonsets[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$peacedealonset==1,]$agname==ag))
  vpeacedeal_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$peacedealongoing==1,]$agname==ag))
  vceasefireonsets[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$ceasefireonset==1,]$agname==ag))
  vceasefire_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$ceasefireongoing==1,]$agname==ag))
  vamnesty_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$amnesty==1,]$agname==ag))
  
  vterminate[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$terminate==1,]$agname==ag))
  
  vsupportGen_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agextsupport_general==1,]$agname==ag))
  vsupportAlg_yrs[ag] <- as.numeric(sum(aosaLLC_long[aosaLLC_long$agextsupport_alleged==1,]$agname==ag))
  
}


# Third, create secondary variables

## Alternative way to calculate the duration of the LLC
vLLC_endLessStart <- as.numeric(vLLC_end) - as.numeric(vLLC_start) + 1

## Indicates if the count of observations for an armed group is less than five years, meaning the group should be removed from the list of LLCs
vLLC_lessThan5yrs <- as.numeric(vLLC_yrs) < 5

## Indicates if the armed group was a mainstream political party for the duration of the LLC, meaning the group should be removed from the list of LLCs
vmainstreamParty <- as.numeric(vnotParty_yrs) == 0

## Indicates if there is a disjuncture between the two methods for counting the duration of the LLC, meaning we need to manually examine the data
vinconsistentDuration <- as.numeric(vLLC_yrs) != vLLC_endLessStart


# Fourth, assemble the matrix of LLCs

aosaLLC <- cbind(as.numeric(vLLC_yrs), 
                 as.numeric(vLLC_start), 
                 as.numeric(vLLC_end), 
                 as.numeric(vLLC_lessThan5yrs), 
                 as.numeric(vLLC_endLessStart), 
                 as.numeric(vinconsistentDuration), 
                 vCountry, 
                 vAG_cleavage, 
                 vreligion_spec, 
                 vID_ideology, 
                 as.numeric(vmaximalGoals_yrs), 
                 as.numeric(vperipheryGoals_yrs), 
                 as.numeric(vmainstreamParty), 
                 as.numeric(vnotParty_yrs), 
                 as.numeric(vnotRebel_yrs), 
                 as.numeric(vagpolwing_yrs), 
                 as.numeric(vformalPolpar_yrs), 
                 as.numeric(vsomePargov_yrs), 
                 as.numeric(vagillicit_yrs), 
                 as.numeric(vagtax_yrs), 
                 as.numeric(vpeacedealonsets), 
                 as.numeric(vpeacedeal_yrs), 
                 as.numeric(vceasefireonsets), 
                 as.numeric(vceasefire_yrs), 
                 as.numeric(vamnesty_yrs), 
                 as.numeric(vterminate), 
                 as.numeric(vsupportGen_yrs), 
                 as.numeric(vsupportAlg_yrs)
                 )

aosaLLC <- (data.frame(row.names(aosaLLC), aosaLLC, row.names = NULL))

colnames(aosaLLC) <- c("agname", 
                       "LLC_yrs", 
                       "LLC_start", 
                       "LLC_end", 
                       "LLC_lessThan5yrs", 
                       "LLC_endLessStart", 
                       "inconsistentDuration", 
                       "Country", 
                       "AG_cleavage", 
                       "religion_spec", 
                       "ID_ideology", 
                       "maximalGoals_yrs", 
                       "peripheryGoals_yrs", 
                       "mainstreamParty", 
                       "notParty_yrs", 
                       "notRebel_yrs", 
                       "agpolwing_yrs", 
                       "formalPolpar_yrs", 
                       "somePargov_yrs", 
                       "agillicit_yrs", 
                       "agtax_yrs", 
                       "peacedealonsets", 
                       "peacedeal_yrs", 
                       "ceasefireonsets", 
                       "ceasefire_yrs", 
                       "amnesty_yrs", 
                       "terminations", 
                       "supportGen_yrs", 
                       "supportAlg_yrs"
                       )

# Fifth, perform the subsetting

aosaLLC <- aosaLLC[ aosaLLC$LLC_lessThan5yrs == 0 & aosaLLC$mainstreamParty == 0,]

```

Third, we are going to identify and manually inspect rows where there may be multiple LLCs. This will include:

* Counting the number of elements

* Manually inspecting rows where there may be multiple LLCs

* Replacing anomalous rows with corrected rows

We will also prepare the matrix for graphics by:

* Removing unnecessary columns

* Preparing unique names for each LLC

```{r}

# First, clean up any inconsistencies in the LLC data


# There are at least 45 armed group LLCs

length(aosaLLC$agname)


# Some of these LLCs may be multiples

aosaLLC[aosaLLC$inconsistentDuration==1,]


# Khun Sa Group has 2 LLCs: 1962-1968 and 1983-1988

as.matrix(aosa[aosa$agname=="Khun Sa Group" & aosa$obsyear >= 1962 & aosa$obsyear <= 1988 & aosa$LCfiveyr_filter==1,"obsyear"])

# We will replace the Khun Sa Group row with two new rows

aosaLLC <- aosaLLC[aosaLLC$agname != "Khun Sa Group",]

aosaLLC <- rbind(aosaLLC, 
                 c("Khun Sa Group", 7, 1962, 1968, 0, 7, 0, "Myanmar", "Criminal (Myanmar)", "NA", "none", 0, 0, 0, 7, 7, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 1, 0, 0), 
                 c("Khun Sa Group", 6, 1983, 1988, 0, 6, 0, "Myanmar", "Criminal (Myanmar)", "NA", "none", 6, 6, 0, 6, 6, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 1, 0))


# KNO has 2 LLCs: 1988-1992 and 2006-2016

as.matrix(aosa[aosa$agname=="KNO" & aosa$obsyear >= 1988 & aosa$obsyear <= 2016 & aosa$LCfiveyr_filter==1,"obsyear"])

# We will replace the KNO row with two new rows

aosaLLC <- aosaLLC[aosaLLC$agname != "KNO",]

aosaLLC <- rbind(aosaLLC, 
                 c("KNO", 5, 1988, 1992, 0, 5, 0, "India", "Kuki", "Northeast", "linguistic-tribal", 5, 5, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                 c("KNO", 11, 2006, 2016, 0, 11, 0, "India", "Kuki", "Northeast", "linguistic-tribal", 2, 11, 0, 11, 9, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0))


# NMSP has 1 LLC: 1996-2009

as.matrix(aosa[aosa$agname=="NMSP" & aosa$obsyear >= 1996 & aosa$obsyear <= 2016 & aosa$LCfiveyr_filter==1,"obsyear"])

# We will replace the NMSP row

aosaLLC <- aosaLLC[aosaLLC$agname != "NMSP",]

aosaLLC <- rbind(aosaLLC, 
                 c("NMSP", 14, 1996, 2009, 0, 14, 0, "Myanmar", "Mon", "NA", "linguistic", 0, 0, 0, 14, 0, 14, 0, 0, 0, 0, 0, 0, 0, 14, 0, 0, 0, 0))

# We will also recode the five year LLC filter from 2013 to 2016 to be 0

aosa[aosa$agnameobsyear == "NMSP2013","LCfiveyr_filter"] <- 0
aosa[aosa$agnameobsyear == "NMSP2014","LCfiveyr_filter"] <- 0
aosa[aosa$agnameobsyear == "NMSP2015","LCfiveyr_filter"] <- 0
aosa[aosa$agnameobsyear == "NMSP2016","LCfiveyr_filter"] <- 0


# RSS has 3 LLCs: 1950-1974, 1980-1991, and 2005-2013

as.matrix(aosa[aosa$agname=="RSS" & aosa$obsyear >= 1950 & aosa$obsyear <= 2013 & aosa$LCfiveyr_filter==1,"obsyear"])

# We will replace the RSS row with three new rows

aosaLLC <- aosaLLC[aosaLLC$agname != "RSS",]

aosaLLC <- rbind(aosaLLC, 
                 c("RSS", 25, 1950, 1974, 0, 25, 0, "India", "Hindutva", "Hindu", "religious-linguistic", 0, 0, 0, 25, 25, 25, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                 c("RSS", 12, 1980, 1991, 0, 12, 0, "India", "Hindutva", "Hindu", "religious-linguistic", 0, 0, 0, 12, 12, 12, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
                 c("RSS", 9, 2005, 2013, 0, 9, 0, "India", "Hindutva", "Hindu", "religious-linguistic", 0, 0, 0, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))


# Second, clear matrix by removing columns that were used for filtering but that are now unnecessary

aosaLLC <- aosaLLC[
  ,
  c("agname", 
    "LLC_yrs", 
    "LLC_start", 
    "LLC_end", 
    "Country", 
    "AG_cleavage", 
    "religion_spec", 
    "ID_ideology", 
    "maximalGoals_yrs", 
    "peripheryGoals_yrs", 
    "notParty_yrs", 
    "notRebel_yrs", 
    "agpolwing_yrs", 
    "formalPolpar_yrs", 
    "somePargov_yrs", 
    "agillicit_yrs", 
    "agtax_yrs", 
    "peacedealonsets", 
    "peacedeal_yrs", 
    "ceasefireonsets", 
    "ceasefire_yrs", 
    "amnesty_yrs", 
    "terminations", 
    "supportGen_yrs", 
    "supportAlg_yrs")
  ]


# Third, transform variables measured in "number of years" into variables measured in "% of LLC years"

## % of years with maximalist goals
aosaLLC$maximalGoals_pctyrs <- round(as.numeric(aosaLLC$maximalGoals_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with goals on the territorial periophery
aosaLLC$peripheryGoals_pctyrs <- round(as.numeric(aosaLLC$peripheryGoals_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)


## % of years NOT as a mainstream political party
aosaLLC$notParty_pctyrs <- round(as.numeric(aosaLLC$notParty_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years NOT as an anti-state rebel
aosaLLC$notRebel_pctyrs <- round(as.numeric(aosaLLC$notRebel_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with a political wing
aosaLLC$agpolwing_pctyrs <- round(as.numeric(aosaLLC$agpolwing_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with formal political involvement
aosaLLC$formalPolpar_pctyrs <- round(as.numeric(aosaLLC$formalPolpar_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with some level of parallel government
aosaLLC$somePargov_pctyrs <- round(as.numeric(aosaLLC$somePargov_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years involved in illicit economies
aosaLLC$agillicit_pctyrs <- round(as.numeric(aosaLLC$agillicit_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years involved in taxation
aosaLLC$agtax_pctyrs <- round(as.numeric(aosaLLC$agtax_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)


## % of years under a peace deal
aosaLLC$peacedeal_pctyrs <- round(as.numeric(aosaLLC$peacedeal_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years under a ceasefire
aosaLLC$ceasefire_pctyrs <- round(as.numeric(aosaLLC$ceasefire_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with an amnesty
aosaLLC$amnesty_pctyrs <- round(as.numeric(aosaLLC$amnesty_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years with external support
aosaLLC$supportGen_pctyrs <- round(as.numeric(aosaLLC$supportGen_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)

## % of years allegedly with external support
aosaLLC$supportAlg_pctyrs <- round(as.numeric(aosaLLC$supportAlg_yrs) / as.numeric(aosaLLC$LLC_yrs), 3)


# Fourth, recompile the list of LLCs - once in terms of % of years, and again in terms of count of years

aosaLLC_pctyrs <- aosaLLC[
  ,
  c("agname", 
    "LLC_yrs", 
    "LLC_start", 
    "LLC_end", 
    "Country", 
    "AG_cleavage", 
    "religion_spec", 
    "ID_ideology", 
    "maximalGoals_pctyrs", 
    "peripheryGoals_pctyrs", 
    "notParty_pctyrs", 
    "notRebel_pctyrs", 
    "agpolwing_pctyrs", 
    "formalPolpar_pctyrs", 
    "somePargov_pctyrs", 
    "agillicit_pctyrs", 
    "agtax_pctyrs", 
    "peacedealonsets", 
    "peacedeal_pctyrs", 
    "ceasefireonsets", 
    "ceasefire_pctyrs", 
    "amnesty_pctyrs", 
    "terminations", 
    "supportGen_pctyrs", 
    "supportAlg_pctyrs")
  ]

aosaLLC <- aosaLLC[
  ,
  c("agname", 
    "LLC_yrs", 
    "LLC_start", 
    "LLC_end", 
    "Country", 
    "AG_cleavage", 
    "religion_spec", 
    "ID_ideology", 
    "maximalGoals_yrs", 
    "peripheryGoals_yrs", 
    "notParty_yrs", 
    "notRebel_yrs", 
    "agpolwing_yrs", 
    "formalPolpar_yrs", 
    "somePargov_yrs", 
    "agillicit_yrs", 
    "agtax_yrs", 
    "peacedealonsets", 
    "peacedeal_yrs", 
    "ceasefireonsets", 
    "ceasefire_yrs", 
    "amnesty_yrs", 
    "terminations", 
    "supportGen_yrs", 
    "supportAlg_yrs")
  ]


# Fifth, generate a list of unique LLCs (Armed Group Name + Country + Years) and bind it as a new column

# NOTE: do not run this multiple times, as it will simply keep adding LLC names to the dataset

aosaLLC <- cbind(
  paste(
    aosaLLC$agname, 
    " and ", 
    aosaLLC$Country, 
    " (", 
    aosaLLC$LLC_start, 
    "-", 
    aosaLLC$LLC_end, 
    ")", 
    sep = ""
    )
  , 
  aosaLLC
  )

colnames(aosaLLC)[1] <- "LLC"

aosaLLC_pctyrs <- cbind(
  paste(
    aosaLLC_pctyrs$agname, 
    " and ", 
    aosaLLC_pctyrs$Country, 
    " (", 
    aosaLLC_pctyrs$LLC_start, 
    "-", 
    aosaLLC_pctyrs$LLC_end, 
    ")", 
    sep = ""
    )
  , 
  aosaLLC_pctyrs
  )

colnames(aosaLLC_pctyrs)[1] <- "LLC"

```

Fourth, we'll create the dataset of LLCs over ten years.

```{r}

# List of LLCs 10 years or longer in duration

aosaLLC_over10yrs <- aosaLLC[aosaLLC$LLC_yrs>=10,]

aosaLLC_over10yrs_pctyrs <- aosaLLC_pctyrs[aosaLLC_pctyrs$LLC_yrs>=10,]

(LLCs_over10yrs <- sort(aosaLLC_over10yrs$LLC))

```

Fifth, we'll recode the dyads in "aosa_notParty" that are different from the dyads in "aosaLLC" and "aosaLLC_over10yrs"

```{r}

# Five year LLC

## First, the dyad-years set seems to contain two additional dyads than the LLC set

length(unique(aosa_notParty[aosa_notParty$LCfiveyr_filter==1,]$agname))

length(unique(aosaLLC$agname))

## Second, these two dyads contained in the dyad-years set seem to be ALP and HQN-Pakistan

setdiff(unique(aosa_notParty[aosa_notParty$LCfiveyr_filter==1,]$agname),
        unique(aosaLLC$agname))

setdiff(unique(aosaLLC$agname),
        unique(aosa_notParty[aosa_notParty$LCfiveyr_filter==1,]$agname))

## Third, we can identify which years to recode the filter to 0

### ALP: the years 2013-2016 were coded as LLC due to cutoff at 2016

aosa_notParty[aosa_notParty$agname == "ALP", 
              c("agnameobsyear", "armedorder", "neworder", "LCfiveyr_filter")]

aosa_notParty[aosa_notParty$agnameobsyear == "ALP2013","LCfiveyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "ALP2014","LCfiveyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "ALP2015","LCfiveyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "ALP2016","LCfiveyr_filter"] <- 0

### HQN-Pakistan: the years 2014-2016 were coded as LLC due to cutoff at 2016

aosa_notParty[aosa_notParty$agname == "HQN-Pakistan", 
              c("agnameobsyear", "armedorder", "neworder", "LCfiveyr_filter")]

aosa_notParty[aosa_notParty$agnameobsyear == "HQN-Pakistan2014","LCfiveyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "HQN-Pakistan2015","LCfiveyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "HQN-Pakistan2016","LCfiveyr_filter"] <- 0

## Fourth, we will reconfirm that the number of years adds up

sum( as.numeric(unlist(aosaLLC$notParty_yrs)) ) ==
  length(unlist(aosa_notParty[aosa_notParty$LCfiveyr_filter == 1 ,"agnameobsyear"]))


# Ten year LLC

## First, the dyad-years set seems to contain one additional dyad than the LLC set

length(unique(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$agname))

length(unique(aosaLLC_over10yrs$agname))

## Second, this dyad contained in the dyad-years set seems to be the MNG

setdiff(unique(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$agname),
        unique(aosaLLC_over10yrs$agname))

setdiff(unique(aosaLLC_over10yrs$agname),
        unique(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$agname))

## Third, for the MNG, the years 2008 to 2016 were coded as LLC due to the cutoff at 2016

aosa_notParty[aosa_notParty$agname == "MNG", 
              c("agnameobsyear", "armedorder", "neworder", "LCtenyr_filter")]

aosa_notParty[aosa_notParty$agnameobsyear == "MNG2008","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2009","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2010","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2011","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2012","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2013","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2014","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2015","LCtenyr_filter"] <- 0
aosa_notParty[aosa_notParty$agnameobsyear == "MNG2016","LCtenyr_filter"] <- 0

## Fourth, we will reconfirm that the number of years adds up

sum( as.numeric(unlist(aosaLLC_over10yrs$notParty_yrs)) ) ==
  length(unlist(aosa_notParty[aosa_notParty$LCtenyr_filter == 1 ,"agnameobsyear"]))

```


# 3. Preparation: Dataset for Dyad-Year Analysis

This section prepares the dataset used for analysis when the "dyad-year" is the unit of observation. 

First, we'll add in the variables that might be useful for the analysis. Not all variables will be represented in the final figures and tables. 

```{r}

# Armed Group Goals

maximalGoals <- as.numeric( (aosa_notParty$aggoals == 1 | aosa_notParty$aggoals2 == 1)
                            | (aosa_notParty$aggoals == 3 | aosa_notParty$aggoals2 == 3) 
                            )

peripheryGoals <- as.numeric((aosa_notParty$aggoals == 1 | aosa_notParty$aggoals2 == 1)
                            | (aosa_notParty$aggoals == 2 | aosa_notParty$aggoals2 == 2)
                            )

aosa_notParty <- cbind(aosa_notParty , maximalGoals , peripheryGoals)


# Political and Organizational Characteristics of Armed Groups

notRebel <- as.numeric(aosa_notParty$agselfID != 3)

formalPolpar <- as.numeric(aosa_notParty$polpar == 2 | aosa_notParty$polpar == 3)

someParGov <- as.numeric(aosa_notParty$pargov == 2 | aosa_notParty$pargov == 3)

aosa_notParty <- cbind(aosa_notParty, notRebel, formalPolpar, someParGov)


# Fixed Effects and Control Variables

post1988 <- as.numeric(aosa_notParty$obsyear > 1988)

aosa_notParty <- cbind(aosa_notParty, post1988)

```


Second, we'll prepare filters to indicate:

* If the termination was incorporation or disarmament

* If the Indian dyad-years occurred in Northeastern States

* If the Burmese dyad-years occurred with ethnonational cleavages

```{r}

## Indicates whether or not the termination Incorporation or Disarmament

termFilter <- as.numeric(
  aosa_notParty$terminateform == 2 | aosa_notParty$terminateform == 4)

aosa_notParty <- cbind(aosa_notParty , termFilter)

## Indicates if the armed group was in India's Northeast
indiaNortheast <- as.numeric(aosa_notParty$religion_spec == "Northeast")

## Indicates if the armed group was Ethnonational in Myanmar
burmaEthnonational <- as.numeric(aosa_notParty$AG_cleavage == "Chin" 
                                 | aosa_notParty$AG_cleavage == "Kachin" 
                                 | aosa_notParty$AG_cleavage == "Karen" 
                                 | aosa_notParty$AG_cleavage == "Karenni" 
                                 | aosa_notParty$AG_cleavage == "Kokang" 
                                 | aosa_notParty$AG_cleavage == "Mon" 
                                 | aosa_notParty$AG_cleavage == "Naga" 
                                 | aosa_notParty$AG_cleavage == "Palaung" 
                                 | aosa_notParty$AG_cleavage == "Pao" 
                                 | aosa_notParty$AG_cleavage == "Rakhine" 
                                 | aosa_notParty$AG_cleavage == "Shan"
                                 )

aosa_notParty <- cbind(aosa_notParty , indiaNortheast, burmaEthnonational)

```


# 4. Preparation: Datatset for Dyad Analysis

This section prepares the dataset used for analysis when the "dyad" is the unit of observation. 

Notice that I am using the dataset which excludes the mainstream political parties (aosa_notParty), in order to keep consistent with the previous analysis that pooled the dyad-years. 

First, we'll add in the variables useful for the analysis. Not all variables will be represented in the final figures and tables. 

```{r}

## What is the armed group name (armed order name)?
dyadNames <- unique(aosa_notParty$agname)


# Time / armed order / LLC variables

## What is the duration of the dyad (count of years)?
vDuration <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years was the dyad an LLC (at least five years)?
vLLCfiveyr_yrs <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years was the dyad an LLC (at least ten years)?
vLLCtenyr_yrs <- setNames(vector("list",length(dyadNames)), dyadNames)


# Conflict characteristic variables

## In what country does the dyad occur?
vCountry <- setNames(vector("list",length(dyadNames)), dyadNames)

## With which cleavage does armed group identify for most of the dyad?
vAG_cleavage <- setNames(vector("list",length(dyadNames)), dyadNames)

## With which religion does the armed group identify for most of the dyad?
vreligion_spec <- setNames(vector("list",length(dyadNames)), dyadNames)

## With which ideology does the armed group identify for most of the LLC?
vID_ideology <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years did the group identify as leftist?
vLeftist <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years did the group identify as religious?
vReligious <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years did the group identify as linguistic?
vLinguistic <- setNames(vector("list",length(dyadNames)), dyadNames)

## For how many years did the group identify as tribal?
vTribal <- setNames(vector("list",length(dyadNames)), dyadNames)


# Goals of the armed group variables

## For what % of years of the dyad did the armed group espouse at least one maximalist goal?
## aggoals OR aggoals2 == (1) Independence/complete secession OR (3) Control over the central government 
vmaximalGoals_yrs <- setNames(vector("list",length(dyadNames)), dyadNames)

## For what % of years of the dyad did the armed group espouse at least one goal about the territorial periphery?
## aggoals OR aggoals2 == (1) Independence/complete secession OR (2) Autonomy short of secession 
vperipheryGoals_yrs <- setNames(vector("list",length(dyadNames)), dyadNames)


# Party and political activity variables 

## For how many years of the LLC did the armed group NOT identify as "Anti-State Rebel"?
vnotRebel_yrs <- setNames(vector("list",length(dyadNames)), dyadNames)


```

Second, run the loop to populate the variables with their values.

(Note: many errors called "number of items to replace is not a multiple of replacement length" will be returned by the following chunk, but it doesn't seem like this harms the dataset creation process). 

```{r}

for (ag in dyadNames){
  vDuration[ag] <- as.numeric(sum(aosa_notParty$agname==ag))
  vLLCfiveyr_yrs[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$LCfiveyr_filter==1,]$agname==ag))
  vLLCtenyr_yrs[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$agname==ag))
  
  vCountry[ag] <- sapply(aosa_notParty[aosa_notParty$agname==ag,"Country"], Mode)
  vAG_cleavage[ag] <- sapply(aosa_notParty[aosa_notParty$agname==ag,"AG_cleavage"], Mode)
  vreligion_spec[ag] <- sapply(aosa_notParty[aosa_notParty$agname==ag,"religion_spec"], Mode)
  vID_ideology[ag] <- sapply(aosa_notParty[aosa_notParty$agname==ag,"ID_ideology"], Mode)
  vLeftist[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$leftist==1,]$agname==ag))
  vReligious[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$religious==1,]$agname==ag))
  vLinguistic[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$linguistic==1,]$agname==ag))
  vTribal[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$tribal==1,]$agname==ag))
  
  vmaximalGoals_yrs[ag] <- as.numeric(sum(aosa_notParty[((aosa_notParty$aggoals == 1 | aosa_notParty$aggoals2 == 1)|(aosa_notParty$aggoals == 3 | aosa_notParty$aggoals2 == 3)),]$agname==ag))
  vperipheryGoals_yrs[ag] <- as.numeric(sum(aosa_notParty[((aosa_notParty$aggoals == 1 | aosa_notParty$aggoals2 == 1)|(aosa_notParty$aggoals == 2 | aosa_notParty$aggoals2 == 2)),]$agname==ag))

  vnotRebel_yrs[ag] <- as.numeric(sum(aosa_notParty[aosa_notParty$agselfID!=3,]$agname==ag))

}

```

Third, create secondary variables

```{r}

## Indicates if there was at least one year of LLC

vLLCfiveyr_filter <- as.numeric(vLLCfiveyr_yrs > 0)

vLLCtenyr_filter <- as.numeric(vLLCtenyr_yrs > 0)

## Indicates what percent of years during the dyad was spent in LLC

vLLCfiveyr_pctyrs <- round(as.numeric(vLLCfiveyr_yrs)/as.numeric(vDuration), 3)

vLLCtenyr_pctyrs <- round(as.numeric(vLLCtenyr_yrs)/as.numeric(vDuration), 3)

## Indicates if the armed group was in India's Northeast
vIndiaNortheast <- as.numeric(vreligion_spec == "Northeast")

## Indicates if the armed group was Ethnonational in Myanmar
vBurmaEthnonational <- as.numeric(vAG_cleavage == "Chin" 
                                 | vAG_cleavage == "Kachin" 
                                 | vAG_cleavage == "Karen" 
                                 | vAG_cleavage == "Karenni" 
                                 | vAG_cleavage == "Kokang" 
                                 | vAG_cleavage == "Mon" 
                                 | vAG_cleavage == "Naga" 
                                 | vAG_cleavage == "Palaung" 
                                 | vAG_cleavage == "Pao" 
                                 | vAG_cleavage == "Rakhine" 
                                 | vAG_cleavage == "Shan"
                                 )

## Indicates what percent of the years during the dyad was spent in each ideology

vleftist_pctyrs <- round(as.numeric(vLeftist)/as.numeric(vDuration), 3)

vreligious_pctyrs <- round(as.numeric(vReligious)/as.numeric(vDuration), 3)

vlinguistic_pctyrs <- round(as.numeric(vLinguistic)/as.numeric(vDuration), 3)

vtribal_pctyrs <- round(as.numeric(vTribal)/as.numeric(vDuration), 3)

## Indicates if the dyad had at least one year in an ideology

vleftist_filter <- as.numeric(vleftist_pctyrs != 0)

vreligious_filter <- as.numeric(vreligious_pctyrs != 0)

vlinguistic_filter <- as.numeric(vlinguistic_pctyrs != 0)

vtribal_filter <- as.numeric(vtribal_pctyrs != 0)

## Indicates what percent of the years during the dyad was spent pursuing certain goals

vmaximalGoals_pctyrs <- round(as.numeric(vmaximalGoals_yrs)/as.numeric(vDuration), 3)

vperipheryGoals_pctyrs <- round(as.numeric(vperipheryGoals_yrs)/as.numeric(vDuration), 3)

## Indicates if the dyad had at least one year purusing certain goals

vmaximalGoals_filter <- as.numeric(vmaximalGoals_pctyrs != 0)

vperipheryGoals_filter <- as.numeric(vperipheryGoals_pctyrs != 0)

## Indicates what percent of the years during the dyad was spent as a non-rebel

vnotRebel_pctyrs <- round(as.numeric(vnotRebel_yrs)/as.numeric(vDuration), 3)

```

Fourth, assemble the matrix of dyads

```{r}

aosaDyads_notParty <- cbind(as.numeric(vDuration), 
                 as.numeric(vLLCfiveyr_filter),
                 as.numeric(vLLCtenyr_filter),
                 as.numeric(vLLCfiveyr_pctyrs),
                 as.numeric(vLLCtenyr_pctyrs),
                 vCountry, 
                 vAG_cleavage,
                 as.numeric(vBurmaEthnonational),
                 vreligion_spec,
                 as.numeric(vIndiaNortheast),
                 vID_ideology, 
                 as.numeric(vleftist_pctyrs),
                 as.numeric(vreligious_pctyrs),
                 as.numeric(vlinguistic_pctyrs),
                 as.numeric(vtribal_pctyrs),
                 as.numeric(vleftist_filter),
                 as.numeric(vreligious_filter),
                 as.numeric(vlinguistic_filter),
                 as.numeric(vtribal_filter),
                 as.numeric(vmaximalGoals_pctyrs), 
                 as.numeric(vperipheryGoals_pctyrs), 
                 as.numeric(vmaximalGoals_filter), 
                 as.numeric(vperipheryGoals_filter),
                 as.numeric(vnotRebel_pctyrs)
                 )

aosaDyads_notParty <- (data.frame(row.names(aosaDyads_notParty), aosaDyads_notParty, row.names = NULL))

colnames(aosaDyads_notParty) <- c("agname",
                       "Dyad_yrs", 
                       "LLCfiveyr_filter",
                       "LLCtenyr_filter",
                       "LLCfiveyr_pctyrs",
                       "LLCtenyr_pctyrs",
                       "Country", 
                       "AG_cleavage", 
                       "burmaEthnonational",
                       "religion_spec", 
                       "indiaNortheast",
                       "ID_ideology", 
                       "leftist_pctyrs",
                       "religious_pctyrs",
                       "linguistic_pctyrs",
                       "tribal_pctyrs",
                       "leftist_filter",
                       "religious_filter",
                       "linguistic_filter",
                       "tribal_filter",
                       "maximalGoals_pctyrs", 
                       "peripheryGoals_pctyrs", 
                       "maximalGoals_filter",
                       "peripheryGoals_filter",
                       "notRebel_pctyrs" 
                       )


```


# 5. Preparation: List of Terminations (Incorporation and Disarmament)

First, we'll perform an initial subset on the aosa_notParty dataset:

* Include only terminations in the form of Incorporation (2) or Disarmament (4)

Simultaneously, we will only keep variables that might be useful for analysis.

```{r}

aosaTerm <- aosa_notParty[
  (aosa_notParty[,"terminateform"]==2 | aosa_notParty[,"terminateform"]==4)
  , 
  c("agname",
    "obsyear", 
    "terminateform", 
    "Country", 
    "AG_cleavage", 
    "religion_spec", 
    "ID_ideology", 
    "aggoals", 
    "aggoals2", 
    "agselfID", 
    "agpolwing", 
    "polpar", 
    "pargov", 
    "agillicit", 
    "agtax", 
    "peacedealonset", 
    "peacedealongoing", 
    "ceasefireonset", 
    "ceasefireongoing", 
    "amnesty", 
    "LCtenyr_filter", 
    "agextsupport_general", 
    "agextsupport_alleged"
    )
  ]

aosaTerm$terminateform[aosaTerm$terminateform == 2] <- "Incorporation"

aosaTerm$terminateform[aosaTerm$terminateform == 4] <- "Disarmament"

```

Second, we will prepare the list of terminations

```{r}

aosaTerm <- cbind(
  paste(
    aosaTerm$agname, 
    " with ", 
    aosaTerm$Country, 
    " (", 
    aosaTerm$obsyear, 
    ") - ", 
    aosaTerm$terminateform, 
    sep = ""
    )
  , 
  aosaTerm
  )

colnames(aosaTerm)[1] <- "Termination"

aosaTerm <- aosaTerm[order(aosaTerm[,"terminateform"], decreasing = FALSE),]

(terminations <- aosaTerm$Termination)
```


# 6. Figures

## Figure 1: Conflict Stabilization Trajectories

### Figure 1: Conflict Stabilization Trajectories

\begin{tikzpicture}[grow=right]
    \tikzstyle{level 1}=[level distance=45mm,sibling distance=45mm]
    \node[align=center]{Government's \\Political Space \\for an Agreement}
        child{node{No Stabilization} edge from parent node[left]{\textit{closed}}}
        child{node[align=center]{Armed Group's \\Relative Power}
            child{node[align=center]{Incorporation \\or Disarmament} 
                edge from parent node[left]{\textit{weak}}}
            child{node[align=center]{Long-term \\Limited Cooperation}
                edge from parent node[left]{\textit{strong}}}
            edge from parent node[left]{\textit{open}}
            };
\end{tikzpicture}


## Figures 2 and 4: LLC, Incorporation, and Disarmament Onsets in Myanmar (India)

Yearly LLC onsets (10+ years), by country

```{r}

# Prepare vectors to populate with onsets

yrvec <- c(1946:2016)

LLCheights <- c(1:length(yrvec))

names(LLCheights) <- yrvec

# India

for(i in 1:length(yrvec)){
  
  LLCheights[i] <- length(aosaLLC_over10yrs[aosaLLC_over10yrs$LLC_start == yrvec[i] & aosaLLC_over10yrs$Country == "India",]$agname)
  
}

LLCStartCount_India <- LLCheights

# Myanmar

for(i in 1:length(yrvec)){
  
  LLCheights[i] <- length(aosaLLC_over10yrs[aosaLLC_over10yrs$LLC_start == yrvec[i] & aosaLLC_over10yrs$Country == "Myanmar",]$agname)
  
}

LLCStartCount_Myanmar <- LLCheights

# Pakistan

## there are only 2 which occur in 2001 and 2002 respectively

# Bangladesh

## none

```

Yearly incorporations and disarmaments, by country

```{r}

# Prepare vectors to populate with onsets

yrvec <- c(1946:2016)

Termheights <- c(1:length(yrvec))

names(Termheights) <- yrvec

# India

for(i in 1:length(yrvec)){
  
  Termheights[i] <- length(aosaTerm[aosaTerm$obsyear == yrvec[i] & aosaTerm$Country == "India",]$agname)
  
}

TermCount_India <- Termheights

barplot(TermCount_India,
        main = "Incorporations and Disarmaments in India",
        xlab = "Years", ylab = "Number of Incorporations and Disarmaments",
        ylim = c(0,5)
        )

# Myanmar

for(i in 1:length(yrvec)){
  
  Termheights[i] <- length(aosaTerm[aosaTerm$obsyear == yrvec[i] & aosaTerm$Country == "Myanmar",]$agname)
  
}

TermCount_Myanmar <- Termheights

barplot(TermCount_Myanmar,
        main = "Incorporations and Disarmaments in Myanmar",
        xlab = "Years", ylab = "Number of Incorporations and Disarmaments",
        ylim = c(0,5)
        )

# Pakistan

## there is only 1 which occurs in 1984

# Bangladesh

## there are only 2 which occur in 1975 and 1980 respectively

```

Onset of both LLCs and Incorporations/Disarmaments for India and Myanmar

```{r}
# India

barplot(rbind(LLCStartCount_India, TermCount_India),
        main = "Figure 4: LLC, Incorporation, and Disarmament Onsets in India",
        xlab = "Years", ylab = "Number of Onsets",
        ylim = c(0,5),
        legend = c("LLC (10+ years)", "Incorporation and Disarmament"),
        args.legend = list(bty = "n", x = 40, y = 5)
        )

# Myanmar

barplot(rbind(LLCStartCount_Myanmar, TermCount_Myanmar),
        main = "Figure 2: LLC, Incorporation, and Disarmament Onsets in Myanmar",
        xlab = "Years", ylab = "Number of Onsets",
        ylim = c(0,5),
        legend = c("LLC (10+ years)", "Incorporation and Disarmament"),
        args.legend = list(bty = "n", x = 40, y = 5)
        )
```


## Figure 3: Duration of Long-Term Limited Cooperation

```{r}

aosaLLC_hist <- hist(as.numeric(aosaLLC$LLC_yrs), 
     main = "Figure 3: Duration of Long-Term Limited Cooperation",
     xlab = "Duration (in years)", ylab = "Number of LLCs",
     ylim = c(0,30),
     right = FALSE
     )

text(aosaLLC_hist$mids,aosaLLC_hist$counts,labels=aosaLLC_hist$counts, adj=c(0.5, -0.5))

aosaLLC_hist <- plot(aosaLLC_hist, 
                     col=c("white", "grey")[cut(aosaLLC_hist$breaks, c(5, 9, 30))],
                     main = "Figure 3: Duration of Long-Term Limited Cooperation",
                     xlab = "Years", ylab = "Number of LLCs",
                     ylim = c(0,30),
                     labels = TRUE
                     )

```


# 7. Tables

## Table 1: Armed Orders in South Asia

"Raw" Dataset: 

  See section: "1.A. Descriptive statistics of "raw" dataset"


Analyzed Subset:

```{r}

# Earliest year

min(aosa_notParty$obsyear)

# Latest year

max(aosa_notParty$obsyear)

# Countries

length(unique(aosa_notParty$Country))

# Dyads

length(unique(aosa_notParty$agname))

# Dyad-years

length(aosa_notParty$agname)

## Total War as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder==1,]$agname) 
  /length(aosa_notParty$agname),
  2)

## Containment as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder==2,]$agname) 
  /length(aosa_notParty$agname),
  2)

## Hostilities as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder==3,]$agname) 
  /length(aosa_notParty$agname),
  2)

## Limited Cooperation as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder==4,]$agname) 
  /length(aosa_notParty$agname),
  2)

## Alliance as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder==5,]$agname) 
  /length(aosa_notParty$agname),
  2)

# Terminations

sum(aosa_notParty$terminate)

## Collapse as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==1,]$terminate) 
  /sum(aosa_notParty$terminate),
  2)

## Incorporation as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==2,]$terminate) 
  /sum(aosa_notParty$terminate),
  2)

## Absorption as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==3,]$terminate) 
  /sum(aosa_notParty$terminate),
  2)

## Disarmament as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==4,]$terminate) 
  /sum(aosa_notParty$terminate),
  2)

## Victory as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==5,]$terminate) 
  /sum(aosa_notParty$terminate),
  2)

```


Anti-State Rebels Sub-subset

```{r}

# Earliest year

min(aosa_notParty[aosa_notParty$agselfID==3,]$obsyear)

# Latest year

max(aosa_notParty[aosa_notParty$agselfID==3,]$obsyear)

# Countries

length(unique(aosa_notParty[aosa_notParty$agselfID==3,]$Country))

# Dyads

length(unique(aosa_notParty[aosa_notParty$agselfID==3,]$agname))

# Dyad-years

length(aosa_notParty[aosa_notParty$agselfID==3,]$agname)

## Total War as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder == 1 
                       & aosa_notParty$agselfID==3,]$agname)
  /length(aosa_notParty[aosa_notParty$agselfID==3,]$agname),
  2)

## Containment as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder == 2 
                       & aosa_notParty$agselfID==3,]$agname)
  /length(aosa_notParty[aosa_notParty$agselfID==3,]$agname),
  2)

## Hostilities as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder == 3 
                       & aosa_notParty$agselfID==3,]$agname)
  /length(aosa_notParty[aosa_notParty$agselfID==3,]$agname),
  2)

## Limited Cooperation as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder == 4 
                       & aosa_notParty$agselfID==3,]$agname)
  /length(aosa_notParty[aosa_notParty$agselfID==3,]$agname),
  2)

## Alliance as % of Dyad-years

round(
  length(aosa_notParty[aosa_notParty$armedorder == 5 
                       & aosa_notParty$agselfID==3,]$agname)
  /length(aosa_notParty[aosa_notParty$agselfID==3,]$agname),
  2)

# Terminations

sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate)

## Collapse as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==1 
                    & aosa_notParty$agselfID==3,]$terminate) 
  /sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate),
  2)

## Incorporation as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==2 
                    & aosa_notParty$agselfID==3,]$terminate) 
  /sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate),
  2)

## Absorption as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==3 
                    & aosa_notParty$agselfID==3,]$terminate) 
  /sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate),
  2)

## Disarmament as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==4 
                    & aosa_notParty$agselfID==3,]$terminate) 
  /sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate),
  2)

## Victory as % of Terminations

round(
  sum(aosa_notParty[aosa_notParty$terminateform==5 
                    & aosa_notParty$agselfID==3,]$terminate) 
  /sum(aosa_notParty[aosa_notParty$agselfID==3,]$terminate),
  2)

```


## Table 2: What Share of Dyad-Years had an Armed Group Espouse a Maximal Goal?

All Countries

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0,]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0,]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$maximalGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1,]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1,]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1,]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1,]$maximalGoals),
  2)


```


India

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "India",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "India",]$maximalGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "India",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "India",]$maximalGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$maximalGoals),
  2)


```


Myanmar

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Myanmar",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Myanmar",]$maximalGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Myanmar",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Myanmar",]$maximalGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar",]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar",]$maximalGoals),
  2)


```


Pakistan

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Pakistan",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Pakistan",]$maximalGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Pakistan",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Pakistan",]$maximalGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Pakistan",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Pakistan",]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Pakistan",]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Pakistan",]$maximalGoals),
  2)


```


Bangladesh

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Bangladesh", ]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Bangladesh", ]$maximalGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Bangladesh", ]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Bangladesh", ]$maximalGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Bangladesh", ]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Bangladesh", ]$maximalGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Bangladesh", ]$maximalGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Bangladesh", ]$maximalGoals),
  2)


```


## Table 3: What Share of Dyads or Dyad-Years had an Armed Group with these Characteristics? (India)

Territory Goals

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "India",]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "India",]$peripheryGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "India",]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "India",]$peripheryGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$peripheryGoals),
  2)

## Stabilization
round(
  sum(
    aosa_notParty[aosa_notParty$termFilter==1 
                  & aosa_notParty$terminate==1 
                  & aosa_notParty$Country == "India",]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$peripheryGoals),
  2)


```


North-East

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
    as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
    & as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast))
  /length(aosaDyads_notParty[
    as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
    & as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$indiaNortheast)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$indiaNortheast),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$indiaNortheast)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$indiaNortheast),
  2)


```


Leftist ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "India"),]$leftist_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "India"),]$leftist_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "India"),]$leftist_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"),]$leftist_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$leftist)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$leftist),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$leftist)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$leftist),
  2)


```


Religious ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "India"), ]$religious_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "India"), ]$religious_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "India"), ]$religious_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"), ]$religious_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$religious)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$religious),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$religious)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$religious),
  2)


```


Ethno-Ling ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "India"), ]$linguistic_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "India"), ]$linguistic_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "India"), ]$linguistic_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"), ]$linguistic_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$linguistic)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$linguistic),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$linguistic)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$linguistic),
  2)


```


Tribal ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "India"),]$tribal_filter))
  /length(aosaDyads_notParty[
    as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
    & as.logical(aosaDyads_notParty$Country == "India"),]$tribal_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "India"),]$tribal_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"),]$tribal_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$tribal)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$tribal),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$tribal)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$tribal),
  2)


```


Number of Observations

```{r}

# Dyads

## Total
length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast)

## Stabilization
length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "India"),]$indiaNortheast)


# Dyad-Years

## Total
length(aosa_notParty[
        aosa_notParty$Country == "India",]$peripheryGoals)

## Stabilization
length(aosa_notParty[
        aosa_notParty$LCtenyr_filter==1 
        & aosa_notParty$Country == "India",]$peripheryGoals)


# Termination Dyad-Years

## Total
length(aosa_notParty[
        aosa_notParty$terminate==1 
        & aosa_notParty$Country == "India",]$peripheryGoals)

## Stabilization
length(aosa_notParty[
        aosa_notParty$termFilter==1 
        & aosa_notParty$terminate==1 
        & aosa_notParty$Country == "India",]$peripheryGoals)


```


## Table 4: What Share of Dyad-Years had a Peace Deal Onset?

All Countries

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0,]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0,]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1,]$peacedealonset),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1,]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1,]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1,]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1,]$peacedealonset),
  2)


```


India

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "India",]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "India",]$peacedealonset),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "India",]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "India",]$peacedealonset),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "India",]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "India",]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "India",]$peacedealonset),
  2)


```


Myanmar

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Myanmar", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Myanmar", ]$peacedealonset),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Myanmar", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Myanmar", ]$peacedealonset),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$peacedealonset),
  2)


```


Pakistan

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Pakistan", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Pakistan", ]$peacedealonset),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Pakistan", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Pakistan", ]$peacedealonset),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Pakistan", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Pakistan", ]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Pakistan", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Pakistan", ]$peacedealonset),
  2)


```


Bangladesh

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Bangladesh", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Bangladesh", ]$peacedealonset),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Bangladesh", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Bangladesh", ]$peacedealonset),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Bangladesh", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Bangladesh", ]$peacedealonset),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Bangladesh", ]$peacedealonset)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Bangladesh", ]$peacedealonset),
  2)


```


## Table 5: QCA of Incorporation and Disarmament Observations

Formed from the case studies (see the Appendix)


## Table 6: What Share of Dyad-Years had an Ongoing Ceasefire?

All Countries

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

```


India

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country== "India"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country== "India"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country== "India"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country== "India"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "India"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "India"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "India"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "India"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

```


Myanmar

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country== "Myanmar"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country== "Myanmar"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country== "Myanmar"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country== "Myanmar"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Myanmar"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Myanmar"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Myanmar"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Myanmar"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

```


Pakistan

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country== "Pakistan"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country== "Pakistan"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country== "Pakistan"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country== "Pakistan"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Pakistan"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Pakistan"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Pakistan"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Pakistan"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

```


Bangladesh

```{r}
# Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country== "Bangladesh"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country== "Bangladesh"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country== "Bangladesh"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country== "Bangladesh"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Bangladesh"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Bangladesh"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country== "Bangladesh"
                    & aosa_notParty$ceasefireongoing!=-999 
                    & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country== "Bangladesh"
                        & aosa_notParty$ceasefireongoing!=-999 
                        & is.na(aosa_notParty$ceasefireongoing)==FALSE, ]$ceasefireongoing),
  2)

```


## Table 7: QCA of Long-Term Limited Cooperation Observations

Formed from the case studies (see the Appendix)


## Table 8: What Share of Dyads or Dyad-Years had an Armed Group with these Characteristics? (Myanmar)

Territory Goals

```{r}

# Dyad-Years

# No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                    & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==0 
                        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals),
  2)

# Stabilization
round(
  sum(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                    & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$LCtenyr_filter==1 
                        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals),
  2)


```


Ethno-National

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$burmaEthnonational)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$burmaEthnonational),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$burmaEthnonational)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$burmaEthnonational),
  2)


```


Leftist ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$leftist_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$leftist_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$leftist_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$leftist_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar",]$leftist)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar",]$leftist),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar",]$leftist)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar",]$leftist),
  2)


```


Religious ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$religious_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$religious_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$religious_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$religious_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$religious)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$religious),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$religious)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$religious),
  2)


```


Ethno-Ling ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$linguistic_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$linguistic_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$linguistic_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$linguistic_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$linguistic)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$linguistic),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$linguistic)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$linguistic),
  2)


```


Tribal ID

```{r}

# Dyads

# No Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$tribal_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==0) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$tribal_filter),
  2)

# Stabilization
round(
  sum(as.numeric(aosaDyads_notParty[
      as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
      & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$tribal_filter))
  /length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$tribal_filter),
  2)


# Termination Dyad-Years

## No Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==0 
                    & aosa_notParty$terminate==1  
                    & aosa_notParty$Country == "Myanmar", ]$tribal)
  /length(aosa_notParty[aosa_notParty$termFilter==0 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$tribal),
  2)

## Stabilization
round(
  sum(aosa_notParty[aosa_notParty$termFilter==1 
                    & aosa_notParty$terminate==1 
                    & aosa_notParty$Country == "Myanmar", ]$tribal)
  /length(aosa_notParty[aosa_notParty$termFilter==1 
                        & aosa_notParty$terminate==1 
                        & aosa_notParty$Country == "Myanmar", ]$tribal),
  2)


```


Number of Observations

```{r}

# Dyads

## Total
length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational)

## Stabilization
length(aosaDyads_notParty[
        as.logical(aosaDyads_notParty$LLCtenyr_filter==1) 
        & as.logical(aosaDyads_notParty$Country == "Myanmar"), ]$burmaEthnonational)


# Dyad-Years

## Total
length(aosa_notParty[
        aosa_notParty$Country == "Myanmar", ]$peripheryGoals)

## Stabilization
length(aosa_notParty[
        aosa_notParty$LCtenyr_filter==1 
        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)


# Termination Dyad-Years

## Total
length(aosa_notParty[
        aosa_notParty$terminate==1 
        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)

## Stabilization
length(aosa_notParty[
        aosa_notParty$termFilter==1 
        & aosa_notParty$terminate==1 
        & aosa_notParty$Country == "Myanmar", ]$peripheryGoals)


```


# 8. Other Queries and Calculations

## 8.A. Terminations coinciding with LLCs

```{r}

# List of Incorporations and Disarmaments that coincide with LLCs

aosaTerm[aosaTerm$LCtenyr_filter==1,]

# List of LLCs that ended in some form of termination

aosaLLC[aosaLLC$terminations==1,] 

```
